package com.zx.pay.wechat.service;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.service.payments.app.AppService;
import com.wechat.pay.java.service.payments.h5.H5Service;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class WeChatPayService {

    @Autowired
    private Config config;

    @Value("${wechat.pay.app-id}")
    private String appId;

    @Value("${wechat.pay.merchant-id}")
    private String merchantId;

    @Value("${wechat.pay.notify-url}")
    private String notifyUrl;

    /**
     * JSAPI支付
     */
    public com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse jsapiPay(String outTradeNo, String description, int amount, String openid) {
        JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(config).build();

        com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest request = new com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest();
        request.setAppid(appId);
        request.setMchid(merchantId);
        request.setNotifyUrl(notifyUrl);
        request.setDescription(description);
        request.setOutTradeNo(outTradeNo);

        com.wechat.pay.java.service.payments.jsapi.model.Amount jsapiAmount = new com.wechat.pay.java.service.payments.jsapi.model.Amount();
        jsapiAmount.setTotal(amount);
        request.setAmount(jsapiAmount);

        com.wechat.pay.java.service.payments.jsapi.model.Payer payer = new com.wechat.pay.java.service.payments.jsapi.model.Payer();
        payer.setOpenid(openid);
        request.setPayer(payer);

        return service.prepayWithRequestPayment(request);
    }

    /**
     * APP支付
     */
    public com.wechat.pay.java.service.payments.app.model.PrepayResponse appPay(String outTradeNo, String description, int amount) {
        AppService service = new AppService.Builder().config(config).build();

        com.wechat.pay.java.service.payments.app.model.PrepayRequest request = new com.wechat.pay.java.service.payments.app.model.PrepayRequest();
        request.setAppid(appId);
        request.setMchid(merchantId);
        request.setNotifyUrl(notifyUrl);
        request.setDescription(description);
        request.setOutTradeNo(outTradeNo);

        com.wechat.pay.java.service.payments.app.model.Amount appAmount = new com.wechat.pay.java.service.payments.app.model.Amount();
        appAmount.setTotal(amount);
        request.setAmount(appAmount);

        return service.prepay(request);
    }

    /**
     * H5支付
     */
    public com.wechat.pay.java.service.payments.h5.model.PrepayResponse h5Pay(String outTradeNo, String description, int amount, String ip) {
        H5Service service = new H5Service.Builder().config(config).build();

        com.wechat.pay.java.service.payments.h5.model.PrepayRequest request = new com.wechat.pay.java.service.payments.h5.model.PrepayRequest();
        request.setAppid(appId);
        request.setMchid(merchantId);
        request.setNotifyUrl(notifyUrl);
        request.setDescription(description);
        request.setOutTradeNo(outTradeNo);

        com.wechat.pay.java.service.payments.h5.model.Amount h5Amount = new com.wechat.pay.java.service.payments.h5.model.Amount();
        h5Amount.setTotal(amount);
        request.setAmount(h5Amount);

        com.wechat.pay.java.service.payments.h5.model.SceneInfo sceneInfo = new com.wechat.pay.java.service.payments.h5.model.SceneInfo();
        sceneInfo.setPayerClientIp(ip);
        com.wechat.pay.java.service.payments.h5.model.H5Info h5Info = new com.wechat.pay.java.service.payments.h5.model.H5Info();
        h5Info.setType("iOS");
        sceneInfo.setH5Info(h5Info);
        request.setSceneInfo(sceneInfo);

        return service.prepay(request);
    }

    /**
     * Native支付
     */
    public com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse nativePay(String outTradeNo, String description, int amount) {
        NativePayService service = new NativePayService.Builder().config(config).build();

        com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest request = new com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest();
        request.setAppid(appId);
        request.setMchid(merchantId);
        request.setNotifyUrl(notifyUrl);
        request.setDescription(description);
        request.setOutTradeNo(outTradeNo);

        com.wechat.pay.java.service.payments.nativepay.model.Amount nativeAmount = new com.wechat.pay.java.service.payments.nativepay.model.Amount();
        nativeAmount.setTotal(amount);
        request.setAmount(nativeAmount);

        return service.prepay(request);
    }

    /**
     * 小程序支付 (与JSAPI支付类似)
     */
    public com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse miniProgramPay(String outTradeNo, String description, int amount, String openid) {
        JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(config).build();

        com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest request = new com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest();
        request.setAppid(appId);
        request.setMchid(merchantId);
        request.setNotifyUrl(notifyUrl);
        request.setDescription(description);
        request.setOutTradeNo(outTradeNo);

        com.wechat.pay.java.service.payments.jsapi.model.Amount jsapiAmount = new com.wechat.pay.java.service.payments.jsapi.model.Amount();
        jsapiAmount.setTotal(amount);
        request.setAmount(jsapiAmount);

        com.wechat.pay.java.service.payments.jsapi.model.Payer payer = new com.wechat.pay.java.service.payments.jsapi.model.Payer();
        payer.setOpenid(openid);
        request.setPayer(payer);

        return service.prepayWithRequestPayment(request);
    }
}